// pages/wholesale/spuDetail/spuDetail.js
let app = getApp();
const fetchSev = require("../../utils/util.js").fetchSev;
const getSpuDetail = require("../../config.js").getSpuDetail; // 品类列表
const addCartUrl = require("../../config.js").addCartUrl;     // 大码下单

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentSpu: {},   // 当前大码
    cartInputShow: false,

    filter: {
      metal_id: app.globalData.currentSpu.metal_id,
      spu_id: app.globalData.currentSpu.spu_id,
      page: 1,
      pagesize: 15,
    },
    skuList: [],      // 小码列表

    totalWeight: '',  // 总重量
    remark: '',       // 备注

    startTime: 0,     // 禁止连续点击“加入购物车”
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 重置数据
    this.setData({ startTime: 0});
    // 当前大码
    this.setData({ currentSpu: app.globalData.currentSpu });
    console.log('当前大码', this.data.currentSpu);
    // 初始化filter
    let filter = {
      metal_id: app.globalData.currentSpu.metal_id,
      spu_id: app.globalData.currentSpu.spu_id,
      page: 1,
      pagesize: 15
    }
    this.setData({ filter: filter });

    // 获取大码详情
    this._getSpuDetail(this.data.filter);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let filter = this.data.filter;
    filter.page++;
    this.setData({filter: filter});
    this._getSpuDetail(this.data.filter);    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  /**
   * 大码表单显示与隐藏
   */
  _cartInputShow: function () {
    this.setData({ cartInputShow: true });
  },
  _cartInputHide: function () {
    this.setData({ cartInputShow: false });
  },
  _totalWeightChange: function (e) {
    let val = e.detail.value;
    this.setData({ totalWeight: val });
  },
  _remarkChange: function (e) {
    let val = e.detail.value;
    this.setData({ remark: val });
  },


  /**
   * 获取小码列表
   */
  _getSpuDetail: function (filter) {
    wx.showLoading({
      title: '加载中',
    });
    let arr = this.data.skuList;
    fetchSev(getSpuDetail, 'GET', filter, function (res) {
      console.log('小码', res)
      if (!res || res.length == 0) {
        wx.showToast({
          title: '数据已加载完毕',
          icon: 'none'
        });
        return;
      }
      console.log('小码详情', res);
      arr = arr.concat(res || []);
      this.setData({ skuList: arr });

      wx.hideLoading();
    }.bind(this));
  },

  /**
   * 点击sku，跳转到小码详细
   */
  _skuClick: function (e) {
    let curSku = e.currentTarget.dataset.item;
    curSku['department'] = this.data.currentSpu.department;
    app.globalData.currentSku = curSku;
    // console.log(e)
    wx.navigateTo({
      url: '../detail/detail',
    });
  },


  /**
   * 大码加入购物车
   */
  _addToCart: function () {
    // 禁止连续点击“加入购物车”
    let now = Date.now();
    if (now - this.data.startTime < 5000) {
      return false;
    }
    this.setData({startTime: now});

    let data = {
      type: "2",
      
      max_weight: this.data.skuList[0].max_weight, 
      min_weight: this.data.skuList[0].min_weight, 
      num: 0, 
      department: this.data.currentSpu.department,
      customer_id: "", 
      wechat_id: "",

      metal_id: this.data.currentSpu.metal_id, 
      spu_id: this.data.currentSpu.spu_id, 
      sku_id: this.data.skuList[0].id || "",  // 默认用第一个sku
      
      remark: this.data.remark, 
      weight: this.data.totalWeight, 
    };
    // 数据校验
    if (data.weight == '' || !data.weight) {
      wx.showToast({
        title: '请输入总重量',
        icon: 'none'
      });
      return false;
    }
    // 加入购物车
    console.log('data', data);
    fetchSev(addCartUrl, 'POST', data, function(res) {
      console.log('加入购物车', res);
      if(res) {
        wx.showToast({
          title: '加入购物车成功',
          icon: 'success',
          duration: 2000,
          complete: function() {
            setTimeout(()=>{
              wx.navigateBack({
                delta: 1
              });
            }, 2000);            
          }
        })        
      }
    });
  }


})